<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author LYS
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button
          type="primary"
          v-if="false"
          @click="handleForm({})"
          v-auth="'cardd:ddrw:carddYcddrw:edit'"
        >
          <Icon icon="i-fluent:add-12-filled" />
          {{ t('新增') }}
        </a-button>
      </template>
      <template #orderColumn="{ record }">
        <a @click="handleOrderForm({ id: record?.carddYcdd?.id })">
          {{ record?.carddYcdd?.ycddDdbh }}
        </a>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.ycddrwRwdh }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <OrderInputForm @register="registerOrderDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsCarddDdrwCarddYcddrwList">
  import { unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicColumn, BasicTable, useTable } from '/@/components/Table';
  import {
    carddYcddrwCancel,
    carddYcddrwDelete,
    carddYcddrwListData,
    carddYcddrwRecover,
  } from '/@/api/cardd/ddrw/carddYcddrw';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import OrderInputForm from '../../order/carddYcdd/form.vue';

  const { t } = useI18n('cardd.ddrw.carddYcddrw');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('用车订单任务管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('任务状态'),
        field: 'ycddrwRwzt',
        component: 'Select',
        componentProps: {
          dictType: 'car_ddzxzt',
          allowClear: true,
        },
      },
      {
        label: t('订单编号'),
        field: 'carddYcdd.ycddDdbh',
        component: 'Input',
      },
      {
        label: t('车单编号'),
        field: 'ycddrwCdbh',
        component: 'Input',
      },
      {
        label: t('单位简称'),
        field: 'cus.khdaDwjc',
        component: 'Input',
      },
      {
        label: t('联系人'),
        field: 'carddYcdd.contact.khlxrLxrmc',
        component: 'Input',
      },
      {
        label: t('车牌号'),
        field: 'carddYcddBt.btCph',
        component: 'Input',
      },
      {
        label: t('品牌型号'),
        field: 'carddYcddBt.btPpxh',
        component: 'Select',
        componentProps: {
          dictType: 'car_type',
          allowClear: true,
        },
      },
      {
        label: t('驾驶员'),
        field: 'carddYcddBt.btJsy',
        component: 'Input',
      },
      {
        label: t('租赁方式'),
        field: 'carddYcddBt.btZlfs',
        component: 'Select',
        componentProps: {
          dictType: 'car_zlfs',
          allowClear: true,
        },
      },
      {
        label: t('开始时间'),
        field: 'btKsrq',
        component: 'RangePicker',
        componentProps: {
          allowClear: true,
        },
      },
    ],
    fieldMapToTime: [['btKsrq', ['btKsrq_gte', 'btKsrq_lte']]],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('订单编号'),
      dataIndex: 'carddYcdd.ycddDdbh',
      key: 'ycdd.ycdd_ddbh',
      sorter: true,
      width: 180,
      align: 'center',
      slot: 'orderColumn',
    },
    {
      title: t('任务单号'),
      dataIndex: 'ycddrwRwdh',
      key: 'a.ycddrw_rwdh',
      sorter: true,
      width: 180,
      align: 'center',
      slot: 'firstColumn',
    },
    {
      title: t('开始时间'),
      dataIndex: 'carddYcddBt.btKsrq',
      // key: 'a.ycddrw_cdbh',
      // sorter: true,
      width: 135,
      align: 'center',
    },
    {
      title: t('结束时间'),
      dataIndex: 'carddYcddBt.btJsrq',
      // key: 'a.ycddrw_cdbh',
      // sorter: true,
      width: 135,
      align: 'center',
    },
    {
      title: t('用车单位'),
      dataIndex: 'carddYcdd.basCus.khdaDwjc',
      // key: 'a.ycddrw_cdbh',
      // sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('联系人'),
      // dataIndex: 'carddYcdd.ycddLxr',
      dataIndex: 'carddYcdd.contact.khlxrLxrmc',
      // key: 'a.ycddrw_cdbh',
      // sorter: true,
      width: 80,
      align: 'center',
    },
    {
      title: t('行程'),
      dataIndex: 'carddYcddBt.btClxc',
      // key: 'a.ycddrw_cdbh',
      // sorter: true,
      width: 120,
      align: 'center',
    },
    {
      title: t('要求'),
      dataIndex: 'carddYcdd.ycddClyq',
      // key: 'a.ycddrw_cdbh',
      // sorter: true,
      width: 120,
      align: 'center',
    },
    {
      title: t('车牌号'),
      dataIndex: 'carddYcddBt.btCph',
      // key: 'a.btCph',
      // sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('驾驶员'),
      dataIndex: 'carddYcddBt.btJsy',
      // key: 'a.btJsy',
      // sorter: true,
      width: 80,
      align: 'center',
    },
    {
      title: t('备注'),
      dataIndex: 'carddYcdd.remarks',
      key: 'ycdd.remarks',
      // sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('联系电话'),
      dataIndex: 'carddYcdd.ycddLxdh',
      // key: 'a.ycddrw_cdbh',
      // sorter: true,
      width: 120,
      align: 'center',
    },
    {
      title: t('租赁方式'),
      dataIndex: 'carddYcddBt.btZlfs',
      // key: 'a.btZlfs',
      // sorter: true,
      width: 130,
      align: 'center',
      dictType: 'car_zlfs',
    },
    {
      title: t('外调类型'),
      dataIndex: 'carddYcddBt.btWdlx',
      // key: 'a.btWdlx',
      // sorter: true,
      width: 130,
      align: 'center',
      dictType: 'car_wdlx',
    },
    {
      title: t('车单编号'),
      dataIndex: 'ycddrwCdbh',
      key: 'a.ycddrw_cdbh',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('品牌型号'),
      dataIndex: 'carddYcddBt.btPpxh',
      // key: 'a.btPpxh',
      // sorter: true,
      width: 130,
      align: 'center',
      dictType: 'car_type',
    },

    {
      title: t('实际公里数'),
      dataIndex: 'ycddrwSjgls',
      key: 'a.ycddrw_sjgls',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('收费公里数'),
      dataIndex: 'ycddrwSfgls',
      key: 'a.ycddrw_sfgls',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('绩效公里数'),
      dataIndex: 'ycddrwJxgls',
      key: 'a.ycddrw_jxgls',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('用车天数'),
      dataIndex: 'ycddrwYcts',
      key: 'a.ycddrw_ycts',
      sorter: true,
      width: 130,
      align: 'center',
    },

    {
      title: t('超里程数'),
      dataIndex: 'ycddrwClcs',
      key: 'a.ycddrw_clcs',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('超里程金额'),
      dataIndex: 'ycddrwClcje',
      key: 'a.ycddrw_clcje',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('超时时间'),
      dataIndex: 'ycddrwCssj',
      key: 'a.ycddrw_cssj',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('超时金额'),
      dataIndex: 'ycddrwCsje',
      key: 'a.ycddrw_csje',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('支出合计'),
      dataIndex: 'ycddrwZchj',
      key: 'a.ycddrw_zchj',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('车费'),
      dataIndex: 'ycddrwCf',
      key: 'a.ycddrw_cf',
      sorter: true,
      width: 130,
      align: 'center',
    },

    {
      title: t('合计金额'),
      dataIndex: 'ycddrwHjje',
      key: 'a.ycddrw_hjje',
      sorter: true,
      width: 130,
      align: 'center',
    },

    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 80,
      align: 'center',
      dictType: 'car_task_status',
      fixed: 'left',
    },
    {
      title: t('任务状态'),
      dataIndex: 'ycddrwRwzt',
      key: 'a.ycddrw_rwzt',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'car_ddzxzt',
      fixed: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑用车订单任务'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'cardd:ddrw:carddYcddrw:edit',
      },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除用车订单任务'),
        popConfirm: {
          title: t('是否确认删除用车订单任务'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'cardd:ddrw:carddYcddrw:edit',
        ifShow: false,
      },
      {
        icon: 'i-ant-design:stop-outlined',
        color: 'error',
        title: t('取消用车订单任务'),
        popConfirm: {
          title: t('是否确认取消用车订单任务'),
          confirm: handleCancel.bind(this, record),
        },
        auth: 'cardd:ddrw:carddYcddrw:edit',
        ifShow: record.status != '1',
      },
      {
        icon: 'i-ant-design:check-circle-outlined',
        color: 'error',
        title: t('恢复用车订单任务'),
        popConfirm: {
          title: t('是否确认恢复用车订单任务'),
          confirm: handleRecover.bind(this, record),
        },
        auth: 'cardd:ddrw:carddYcddrw:edit',
        ifShow: record.status == '1',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerOrderDrawer, { openDrawer: openOrderDrawer }] = useDrawer();
  const [registerTable, { reload }] = useTable({
    api: carddYcddrwListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    // actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    pagination: {
      pageSize: 100,
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  function handleOrderForm(record: Recordable) {
    openOrderDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { id: record.id };
    const res = await carddYcddrwDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleCancel(record: Recordable) {
    const params = { id: record.id };
    const res = await carddYcddrwCancel(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleRecover(record: Recordable) {
    const params = { id: record.id };
    const res = await carddYcddrwRecover(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }
</script>
